package com.icode.core.shared;

import com.icode.core.model.house.HouseWeekTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-6-29
 * Time: 上午9:36
 * To change this template use File | Settings | File Templates.
 */
public class HouseWeekHandle {


    //预定时间不能在当前时间之前
    public static List<HouseWeekTime> exceptUselessHouseWeekTime(List<HouseWeekTime> houseWeekTimes) {
        List<HouseWeekTime> list = new ArrayList<>();
        try {
            for (HouseWeekTime h : houseWeekTimes) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date current = new Date();
                current = getInternalDateByDay(current, 15);
                Date date = sdf.parse(h.start());
                boolean flag = current.after(date);
                if (!flag) {
                    list.add(h);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static String houseWeekTimeToRegular(String[] times) {
        String reg = "";
        for (int i = 0; i < times.length - 1; i++) {
            String start = times[i].split(",")[1];
            String end = times[i + 1].split(",")[0];
            reg += timeToReg(start, end) + ",";
        }
        return reg.substring(0, reg.lastIndexOf(","));
    }

    public static String disableTimeToRegular(String start, String end) {
        String reg = disableRegByDay(start, end);
        return reg;
    }

    public static String weekMinDate(String time) {
        String[] times = time.split(";");
        String minDate = times[0].substring(0, 10);
        return minDate;
    }

    public static String weekMaxDate(String time) {
        String[] times = time.split(";");
        String maxDate = times[times.length - 1].substring(11);
        return maxDate;
    }

    private static int subDay(int day) {
        String sday = String.valueOf(day);
        return Integer.parseInt(sday.substring(1));
    }

    private static String timeToReg(String start, String end) {
        String reg = "";
        int startYear = Integer.parseInt(start.substring(0, 4));
        int startMonth = Integer.parseInt(start.substring(5, 7));
        int startDay = Integer.parseInt(start.substring(8));
        int endYear = Integer.parseInt(end.substring(0, 4));
        int endMonth = Integer.parseInt(end.substring(5, 7));
        int endDay = Integer.parseInt(end.substring(8));
        if (Math.abs(endMonth - startMonth) <= 0) {     //月份相差0个月,当月内进行禁用,此情况下startDay永远不会大于endDay
            if (endDay > 0 && endDay <= 9) {     //endDay为 1-9
                reg += startYear + "-" + start.substring(5, 7) + "-0[" + (startDay + 1) + "-" + (endDay - 1) + "]";
            } else if (endDay > 9 && endDay <= 19) {  //endDay为 10-19
                if (startDay > 0 && startDay <= 9) {    // endDay为 10-19 --> startDay为 0-9
                    if (startDay != 9) {
                        if (endDay != 10) {
                            reg += startYear + "-" + start.substring(5, 7) + "-0[" + (startDay + 1) + "-9],";
                            reg += startYear + "-" + start.substring(5, 7) + "-1[0-" + subDay(endDay - 1) + "]";
                        } else {
                            reg += startYear + "-" + start.substring(5, 7) + "-0[" + (startDay + 1) + "-9]";
                        }
                    } else {
                        reg += startYear + "-" + start.substring(5, 7) + "-1[0-" + subDay(endDay - 1) + "]";
                    }
                } else {   // endDay为 10-19 -->  startDay为 10-19
                    reg += startYear + "-" + start.substring(5, 7) + "-1[" + subDay(startDay + 1) + "-" + subDay(endDay - 1) + "]";
                }
            } else if (endDay > 19 && endDay <= 29) {   //endDay为 20-29
                if (startDay > 0 && startDay <= 9) {    // endDay为 20-29 --> startDay为 0-9
                    if (startDay != 9) {
                        reg += startYear + "-" + start.substring(5, 7) + "-0[" + (startDay + 1) + "-9],";
                    }
                    if (endDay != 20) {
                        reg += startYear + "-" + start.substring(5, 7) + "-1[0-9],";
                        reg += startYear + "-" + start.substring(5, 7) + "-2[0-" + subDay(endDay - 1) + "]";
                    } else {
                        reg += startYear + "-" + start.substring(5, 7) + "-1[0-9]";
                    }
                } else if (startDay > 9 && startDay <= 19) {    // endDay为 20-29 --> startDay为 10-19
                    if (startDay != 19) {
                        if (endDay != 20) {
                            reg += startYear + "-" + start.substring(5, 7) + "-1[" + subDay(startDay + 1) + "-9],";
                            reg += startYear + "-" + start.substring(5, 7) + "-2[0-" + subDay(endDay - 1) + "]";
                        } else {
                            reg += startYear + "-" + start.substring(5, 7) + "-1[" + subDay(startDay + 1) + "-9]";
                        }
                    } else {
                        reg += startYear + "-" + start.substring(5, 7) + "-2[0-" + subDay(endDay - 1) + "]";
                    }
                } else if (startDay > 19 && startDay <= 29) {   // endDay为 20-29 --> startDay为 20-29
                    reg += startYear + "-" + start.substring(5, 7) + "-2[" + subDay(startDay + 1) + "-" + subDay(endDay - 1) + "]";
                }
            } else if (endDay > 29 && endDay <= 31) {   //endDay为 30-31
                if (startDay > 0 && startDay <= 9) {    // endDay为 30-31 --> startDay为 0-9
                    if (startDay != 9) {
                        reg += startYear + "-" + start.substring(5, 7) + "-0[" + (startDay + 1) + "-9],";
                    }
                    reg += startYear + "-" + start.substring(5, 7) + "-1[0-9],";
                    if (endDay != 30) {
                        reg += startYear + "-" + start.substring(5, 7) + "-2[0-9],";
                        reg += startYear + "-" + start.substring(5, 7) + "-3[0-" + subDay(endDay - 1) + "]";
                    } else {
                        reg += startYear + "-" + start.substring(5, 7) + "-2[0-9]";
                    }
                } else if (startDay > 9 && startDay <= 19) {   // endDay为 30-31 --> startDay为 10-19
                    if (startDay != 19) {
                        reg += startYear + "-" + start.substring(5, 7) + "-1[" + subDay(startDay + 1) + "-9],";
                    }
                    if (endDay != 30) {
                        reg += startYear + "-" + start.substring(5, 7) + "-2[0-9],";
                        reg += startYear + "-" + start.substring(5, 7) + "-3[0-" + subDay(endDay - 1) + "]";
                    } else {
                        reg += startYear + "-" + start.substring(5, 7) + "-2[0-9]";
                    }
                } else if (startDay > 19 && startDay <= 29) {       // endDay为 30-31 --> startDay为 20-29
                    if (startDay != 29) {
                        if (endDay != 30) {
                            reg += startYear + "-" + start.substring(5, 7) + "-2[" + subDay(startDay + 1) + "-9],";
                            reg += startYear + "-" + start.substring(5, 7) + "-3[0-" + subDay(endDay - 1) + "]";
                        } else {
                            reg += startYear + "-" + start.substring(5, 7) + "-2[" + subDay(startDay + 1) + "-9]";
                        }
                    } else {
                        reg += startYear + "-" + start.substring(5, 7) + "-3[0-" + subDay(endDay - 1) + "]";
                    }
                }
//                else if (startDay > 29 && startDay <= 31) {  //没有实际意义
//                    reg += startYear + "-" + start.substring(5, 7) + "-3[" + subDay(startDay + 1) + "-" + subDay(endDay - 1) + "]";
//                }
            }
        } else if (Math.abs(endMonth - startMonth) == 1) {   //月份相差1个月,直接禁用两个月的单独日期
            if (startDay > 0 && startDay <= 9) {  //天为 1-9
                if (startDay != 9) {
                    reg += startYear + "-" + start.substring(5, 7) + "-0[" + (startDay + 1) + "-9],";
                }
                reg += startYear + "-" + start.substring(5, 7) + "-1[0-9],";
                reg += startYear + "-" + start.substring(5, 7) + "-2[0-9],";
                reg += startYear + "-" + start.substring(5, 7) + "-3[0-1],";
            } else if (startDay > 9 && startDay <= 19) {    //天为 10-19
                if (startDay != 19) {
                    reg += startYear + "-" + start.substring(5, 7) + "-1[" + subDay(startDay + 1) + "-9],";
                }
                reg += startYear + "-" + start.substring(5, 7) + "-2[0-9],";
                reg += startYear + "-" + start.substring(5, 7) + "-3[0-1],";
            } else if (startDay > 19 && startDay <= 29) {   //天为 20-29
                if (startDay != 29) {
                    reg += startYear + "-" + start.substring(5, 7) + "-2[" + subDay(startDay + 1) + "-9],";
                }
                reg += startYear + "-" + start.substring(5, 7) + "-3[0-1],";
            } else if (startDay > 29 && startDay <= 31) {    //天为 30-31
                reg += startYear + "-" + start.substring(5, 7) + "-3[" + subDay(startDay + 1) + "-1],";
            }

            if (endDay > 0 && endDay <= 9) {    //天为 1-9
                reg += endYear + "-" + end.substring(5, 7) + "-0[0-" + (endDay - 1) + "],";
            } else if (endDay > 9 && endDay <= 19) {    //天为 10-19
                reg += endYear + "-" + end.substring(5, 7) + "-0[0-9],";
                if (endDay != 10) {
                    reg += endYear + "-" + end.substring(5, 7) + "-1[0-" + subDay(endDay - 1) + "],";
                }
            } else if (endDay > 19 && endDay <= 29) {   //天为 20-29
                reg += endYear + "-" + end.substring(5, 7) + "-0[0-9],";
                reg += endYear + "-" + end.substring(5, 7) + "-1[0-9],";
                if (endDay != 20) {
                    reg += endYear + "-" + end.substring(5, 7) + "-2[0-" + subDay(endDay - 1) + "],";
                }
            } else if (endDay > 29 && endDay <= 31) {   //天为 30-31
                reg += endYear + "-" + end.substring(5, 7) + "-0[0-9],";
                reg += endYear + "-" + end.substring(5, 7) + "-1[0-9],";
                if (endDay != 30) {
                    reg += endYear + "-" + end.substring(5, 7) + "-2[0-9],";
                    reg += endYear + "-" + end.substring(5, 7) + "-3[0-" + subDay(endDay - 1) + "]";
                } else {
                    reg += endYear + "-" + end.substring(5, 7) + "-2[0-9]";
                }
            }
        } else {    //月份相差1个月以上,需要禁用中间月份
            int number = endMonth - startMonth;
            int index = 1;
            if (startDay > 0 && startDay <= 9) {  //天为 1-9
                if (startDay != 9) {
                    reg += startYear + "-" + start.substring(5, 7) + "-0[" + (startDay + 1) + "-9],";
                }
                reg += startYear + "-" + start.substring(5, 7) + "-1[0-9],";
                reg += startYear + "-" + start.substring(5, 7) + "-2[0-9],";
                reg += startYear + "-" + start.substring(5, 7) + "-3[0-1],";
            } else if (startDay > 9 && startDay <= 19) {    //天为 10-19
                if (startDay != 19) {
                    reg += startYear + "-" + start.substring(5, 7) + "-1[" + subDay(startDay + 1) + "-9],";
                }
                reg += startYear + "-" + start.substring(5, 7) + "-2[0-9],";
                reg += startYear + "-" + start.substring(5, 7) + "-3[0-1],";
            } else if (startDay > 19 && startDay <= 29) {   //天为 20-29
                if (startDay != 29) {
                    reg += startYear + "-" + start.substring(5, 7) + "-2[" + subDay(startDay + 1) + "-9],";
                }
                reg += startYear + "-" + start.substring(5, 7) + "-3[0-1],";
            } else if (startDay > 29 && startDay <= 31) {    //天为 30-31
                reg += startYear + "-" + start.substring(5, 7) + "-3[" + subDay(startDay + 1) + "-1],";
            }
            while (index < number) {
                if (startMonth < 10) {
                    reg += startYear + "-0" + (startMonth + index) + ",";
                } else {
                    reg += startYear + "-" + (startMonth + index) + ",";
                }
                index = index + 1;
            }
            if (endDay > 0 && endDay <= 9) {    //天为 1-9
                reg += endYear + "-" + end.substring(5, 7) + "-0[0-" + (endDay - 1) + "],";
            } else if (endDay > 9 && endDay <= 19) {    //天为 10-19
                reg += endYear + "-" + end.substring(5, 7) + "-0[0-9],";
                if (endDay != 10) {
                    reg += endYear + "-" + end.substring(5, 7) + "-1[0-" + subDay(endDay - 1) + "],";
                }
            } else if (endDay > 19 && endDay <= 29) {   //天为 20-29
                reg += endYear + "-" + end.substring(5, 7) + "-0[0-9],";
                reg += endYear + "-" + end.substring(5, 7) + "-1[0-9],";
                if (endDay != 20) {
                    reg += endYear + "-" + end.substring(5, 7) + "-2[0-" + subDay(endDay - 1) + "],";
                }
            } else if (endDay > 29 && endDay <= 31) {   //天为 30-31
                reg += endYear + "-" + end.substring(5, 7) + "-0[0-9],";
                reg += endYear + "-" + end.substring(5, 7) + "-1[0-9],";
                if (endDay != 30) {
                    reg += endYear + "-" + end.substring(5, 7) + "-2[0-9],";
                    reg += endYear + "-" + end.substring(5, 7) + "-3[0-" + subDay(endDay - 1) + "]";
                } else {
                    reg += endYear + "-" + end.substring(5, 7) + "-2[0-9]";
                }
            }
        }
        return reg;
    }

    private static String disableRegByDay(String start, String end) {
        String content = start + ",";
        String data = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = sdf.parse(start);
            Date d2 = sdf.parse(end);
            long index = Math.abs(d2.getTime() - d1.getTime()) / 86400000;
            for (int i = 1; i < index; i++) {
                Date d = getInternalDateByDay(d1, i);
                String s = sdf.format(d);
                data += i == index - 1 ? s : s + ",";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if ("".equals(data)) {
            content += end;
        } else {
            content += data + "," + end;
        }
        return content;
    }

    //获取一个日期 多少天以后的日期
    public static Date getInternalDateByDay(Date d, int days) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        now.add(Calendar.DATE, days);
        return now.getTime();
    }


}
